package com.test.burnincash.activity_fragment_burnin.loan_burnin;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentTransaction;

import com.test.burnincash.R;
import com.test.burnincash.activity_fragment_burnin.ApplyPermissionActivity_burnin;
import com.test.burnincash.activity_fragment_burnin.base_burnin.BaseLoanFragment_burnin;
import com.test.burnincash.activity_fragment_burnin.fragment_burnin.DataSafetyFragment_burnin;
import com.test.burnincash.activity_fragment_burnin.fragment_burnin.LoanFailFragment_burnin;
import com.test.burnincash.activity_fragment_burnin.fragment_burnin.LoanMainFragment_burnin;
import com.test.burnincash.activity_fragment_burnin.fragment_burnin.LoanWaitFragment_burnin;
import com.test.burnincash.activity_fragment_burnin.fragment_burnin.PersonalCenterFragment_burnin;
import com.test.burnincash.activity_fragment_burnin.fragment_burnin.RepayFragment_burnin;
import com.test.burnincash.bean_burnin.config_info_burnin.AppShowInfo_burnin;
import com.test.burnincash.bean_burnin.process_burnin.ProcessInfo_burnin;
import com.test.burnincash.dialog_burnin.InitOfflinePayChannelDialog_burnin;
import com.test.burnincash.util_burnin.PermissionUtil_burnin;
import com.test.burnincash.util_burnin.StringUtil_burnin;
import com.test.burnincash.util_burnin.http_burnin.HttpManager_burnin;
import com.test.burnincash.activity_fragment_burnin.base_burnin.BaseLoanActivity_burnin;
import com.test.burnincash.activity_fragment_burnin.info_burnin.InfoUploadActivity_burnin;

import java.util.Objects;

public class LoanMainActivity_burnin extends BaseLoanActivity_burnin {

    private ImageView homeIv, dataSafetyIv, personalCenterIv;
    private TextView homeTv, dataSafetyTv, personalCenterTv;
    private View homeView, dataSafetyView, personalCenterView;

    private int fragmentShow = 1;

    private BaseLoanFragment_burnin homeFragment;
    private DataSafetyFragment_burnin dataSafetyFragment;
    private PersonalCenterFragment_burnin personalCenterFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_loan_main_burnin);
        init();
    }

    private void init(){
        ViewGroup homeLayout = findViewById(R.id.activity_loan_main_layout_home);
        homeIv = findViewById(R.id.activity_loan_main_iv_home);
        homeTv = findViewById(R.id.activity_loan_main_tv_home);
        ViewGroup dataSafetyLayout = findViewById(R.id.activity_loan_main_layout_data_safety);
        dataSafetyIv = findViewById(R.id.activity_loan_main_iv_data_safety);
        dataSafetyTv = findViewById(R.id.activity_loan_main_tv_data_safety);
        ViewGroup personalCenterLayout = findViewById(R.id.activity_loan_main_layout_personal_center);
        personalCenterIv = findViewById(R.id.activity_loan_main_iv_personal_center);
        personalCenterTv = findViewById(R.id.activity_loan_main_tv_personal_center);
        homeView = findViewById(R.id.activity_loan_main_view_home);
        dataSafetyView = findViewById(R.id.activity_loan_main_view_data_safety);
        personalCenterView = findViewById(R.id.activity_loan_main_view_personal_center);

        dataSafetyFragment = new DataSafetyFragment_burnin();
        personalCenterFragment = new PersonalCenterFragment_burnin(this);

        homeLayout.setOnClickListener(v -> changeToHome());
        dataSafetyLayout.setOnClickListener(v -> changeToDataSafety());
        personalCenterLayout.setOnClickListener(v -> changeToPersonalCenter());

    }

    @Override
    protected void onResume() {
        super.onResume();
        updatePage();
    }

    private void updatePage() {

        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();

        if (homeFragment != null) fragmentTransaction.remove(homeFragment);
        if (dataSafetyFragment != null) fragmentTransaction.remove(dataSafetyFragment);
        if (personalCenterFragment != null) fragmentTransaction.remove(personalCenterFragment);

        if (ProcessInfo_burnin.getInstance().isOrderPendingRepayment() || ProcessInfo_burnin.getInstance().isOrderOverdueRepayment()) {//订单状态-待还款
            homeFragment = new RepayFragment_burnin();
        } else if (ProcessInfo_burnin.getInstance().isOrderLoaning() || ProcessInfo_burnin.getInstance().isLoanReviewing()) {//订单状态-放款中/审核中
            homeFragment = new LoanWaitFragment_burnin();
        } else if (ProcessInfo_burnin.getInstance().isLoanRejcted()) {//订单状态-被拒绝
            homeFragment = new LoanFailFragment_burnin();
        } else {
            homeFragment = new LoanMainFragment_burnin();
        }

        fragmentTransaction.add(R.id.activity_loan_main_layout_fragment, Objects.requireNonNull(homeFragment))
                .add(R.id.activity_loan_main_layout_fragment, dataSafetyFragment)
                .add(R.id.activity_loan_main_layout_fragment, personalCenterFragment);

        switch (fragmentShow) {
            case 2:
                fragmentTransaction.show(dataSafetyFragment).hide(homeFragment).hide(personalCenterFragment);
                break;
            case 3:
                fragmentTransaction.show(personalCenterFragment).hide(homeFragment).hide(dataSafetyFragment);
                break;
            default:
                fragmentTransaction.show(homeFragment).hide(dataSafetyFragment).hide(personalCenterFragment);
        }

        fragmentTransaction.commit();
    }

    private void changeToHome() {

        homeIv.setImageResource(R.drawable.icon_tab_home_true);
        dataSafetyIv.setImageResource(R.drawable.icon_tab_data_safety_false);
        personalCenterIv.setImageResource(R.drawable.icon_tab_personal_center_false);

        homeTv.setTextColor(ContextCompat.getColor(this,R.color.black));
        dataSafetyTv.setTextColor(ContextCompat.getColor(this,R.color.gray_dd));
        personalCenterTv.setTextColor(ContextCompat.getColor(this,R.color.gray_dd));

        homeView.setVisibility(View.VISIBLE);
        dataSafetyView.setVisibility(View.INVISIBLE);
        personalCenterView.setVisibility(View.INVISIBLE);

        getSupportFragmentManager().beginTransaction().show(homeFragment).hide(dataSafetyFragment).hide(personalCenterFragment).commit();

        fragmentShow = 1;
    }

    private void changeToDataSafety() {

        homeIv.setImageResource(R.drawable.icon_tab_home_false);
        dataSafetyIv.setImageResource(R.drawable.icon_tab_data_safety_true);
        personalCenterIv.setImageResource(R.drawable.icon_tab_personal_center_false);

        homeTv.setTextColor(ContextCompat.getColor(this,R.color.gray_dd));
        dataSafetyTv.setTextColor(ContextCompat.getColor(this,R.color.black));
        personalCenterTv.setTextColor(ContextCompat.getColor(this,R.color.gray_dd));

        homeView.setVisibility(View.INVISIBLE);
        dataSafetyView.setVisibility(View.VISIBLE);
        personalCenterView.setVisibility(View.INVISIBLE);

        getSupportFragmentManager().beginTransaction().show(dataSafetyFragment).hide(homeFragment).hide(personalCenterFragment).commit();

        fragmentShow = 2;
    }

    private void changeToPersonalCenter() {

        homeIv.setImageResource(R.drawable.icon_tab_home_false);
        dataSafetyIv.setImageResource(R.drawable.icon_tab_data_safety_false);
        personalCenterIv.setImageResource(R.drawable.icon_tab_personal_center_true);

        homeTv.setTextColor(ContextCompat.getColor(this,R.color.gray_dd));
        dataSafetyTv.setTextColor(ContextCompat.getColor(this,R.color.gray_dd));
        personalCenterTv.setTextColor(ContextCompat.getColor(this,R.color.black));

        homeView.setVisibility(View.INVISIBLE);
        dataSafetyView.setVisibility(View.INVISIBLE);
        personalCenterView.setVisibility(View.VISIBLE);

        getSupportFragmentManager().beginTransaction().show(personalCenterFragment).hide(homeFragment).hide(dataSafetyFragment).commit();

        fragmentShow = 3;
    }

    //配置信息返回响应
    public void onAppShowInfoResponse() {
        if (personalCenterFragment != null) personalCenterFragment.updateCustomerService();
    }

    //额度列表返回响应
    public void onLoanLimitInfoResponse() {
        if (homeFragment != null && homeFragment instanceof LoanMainFragment_burnin)
            ((LoanMainFragment_burnin) homeFragment).updateLoanAmount_burnin();
    }

    @Override
    public void onUserProcessResponse_burnin() {
        //判断权限状态，如果存在未获取的权限则跳转到权限申请页
        if (!PermissionUtil_burnin.checkLaunchPermissions(this, AppShowInfo_burnin.getInstance().getHome())) {
            startActivityForSingleTop_burnin(ApplyPermissionActivity_burnin.class);
            return;
        }
        //订单状态-待还款
        if (ProcessInfo_burnin.getInstance().isOrderPendingRepayment() || ProcessInfo_burnin.getInstance().isOrderOverdueRepayment()) {
            if (homeFragment instanceof RepayFragment_burnin) {
                HttpManager_burnin.getInstance().getRepayDetail();
            } else updatePage();
            return;
        }
        //订单状态-审核中/放款中
        if (ProcessInfo_burnin.getInstance().isLoanReviewing() || ProcessInfo_burnin.getInstance().isOrderLoaning()) {
            if (!(homeFragment instanceof LoanWaitFragment_burnin)) updatePage();
            return;
        }
        //订单状态-被拒绝
        if (ProcessInfo_burnin.getInstance().isLoanRejcted()) {
            if (!(homeFragment instanceof LoanFailFragment_burnin)) updatePage();
            return;
        }
        //其他订单状态
        if (homeFragment instanceof LoanMainFragment_burnin) {
            if (((LoanMainFragment_burnin) homeFragment).isRefresh){
                HttpManager_burnin.getInstance().queryLoanLimit();
                ((LoanMainFragment_burnin) homeFragment).isRefresh = false;
            }
            else startLoanProcess();
        } else {
            updatePage();
        }
    }

    //借款流程
    private void startLoanProcess() {

        //科特的绑卡环节
        if (ProcessInfo_burnin.getInstance().getStepsSize()>0){
            startActivity_burnin(InfoUploadActivity_burnin.class);
        }
        else {
            //如果不是处理还款流程、状态流程、认证流程，直接去借款确认页
            startActivity_burnin(LoanApplyActivity_burnin.class);
            HttpManager_burnin.getInstance().requestLoanApplyDetail();
        }

    }

    public void onLoanApplyDetailResponse() {

        //订单状态-放款中/审核中
        if (ProcessInfo_burnin.getInstance().isOrderLoaning() || ProcessInfo_burnin.getInstance().isLoanReviewing()) {
            if ((homeFragment instanceof LoanWaitFragment_burnin)) ((LoanWaitFragment_burnin) homeFragment).onLoanApplyDetailResponse();
        }else startActivity_burnin(LoanApplyActivity_burnin.class);
    }

    public void onRepayDetailResponse() {
        if (homeFragment != null && homeFragment instanceof RepayFragment_burnin)
            ((RepayFragment_burnin) homeFragment).updateRepayDetail();
    }

    public void onOrderRepayH5Response(String url) {
        if (homeFragment != null && homeFragment instanceof RepayFragment_burnin) {
            Uri uri = Uri.parse(StringUtil_burnin.getSafeString(url));
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(intent);
        }
    }

    //获取bankAccountList返回(尼日)
    public void onFetchBankListResponse_Niri(){
        //订单状态-还款中
        if ((ProcessInfo_burnin.getInstance().isOrderPendingRepayment() || ProcessInfo_burnin.getInstance().isOrderOverdueRepayment())) {
            if (homeFragment instanceof RepayFragment_burnin) ((RepayFragment_burnin) homeFragment).onFetchBankListResponse_Niri();
        }
    }

    //获取线下还款通道响应-尼日
    public void onInitOfflinePayChannelResponse() {
        InitOfflinePayChannelDialog_burnin.showDialog(this, dialogInterface -> HttpManager_burnin.getInstance().getRepayDetail());
    }

}